<template>
  <view class="index" :style="{ overflow: 'hidden', height: pageHeight }">
    <!-- 搜索 -->
    <Search @search="disScroll" />
    <!-- 轮播图 -->
    <view class="slider">
      <swiper
        autoplay
        interval="2000"
        circular
        indicator-dots
        indicator-color="rgba(255,255,255,1)"
        indicator-active-color="rgba(255,255,255,.6)"
      >
        <swiper-item v-for="item in swiperList" :key="item.goods_id">
          <navigator :url="`/pageone/goods/index?id=${item.goods_id}`">
            <image :src="item.image_src" />
          </navigator>
        </swiper-item>
      </swiper>
    </view>
    <!-- 功能导航 -->
    <view class="navs">
      <navigator
        :open-type="item.navigator_url ? 'switchTab' : 'navigate'"
        :url="
          item.navigator_url ? '/pages/category/index' : `/pageone/list/index?query=` + item.name
        "
        v-for="item in cateList"
        :key="item.name"
      >
        <image :src="item.image_src" />
      </navigator>
    </view>
    <!-- 栏目楼层 -->
    <view class="floors">
      <!-- 1 -->
      <view class="floor">
        <!-- title -->
        <view class="ftitle">
          <image src="/static/uploads/pic_floor01_title.png" />
        </view>
        <!-- pics -->
        <view class="fitem">
          <navigator url>
            <image src="/static/uploads/pic_floor01_1@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor01_2@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor01_3@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor01_4@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor01_5@2x.png" />
          </navigator>
        </view>
      </view>
      <!-- 2 -->
      <view class="floor">
        <!-- title -->
        <view class="ftitle">
          <image src="/static/uploads/pic_floor02_title.png" />
        </view>
        <!-- pics -->
        <view class="fitem">
          <navigator url>
            <image src="/static/uploads/pic_floor02_1@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor02_2@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor02_3@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor02_4@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor02_5@2x.png" />
          </navigator>
        </view>
      </view>
      <!-- 3 -->
      <view class="floor">
        <!-- title -->
        <view class="ftitle">
          <image src="/static/uploads/pic_floor03_title.png" />
        </view>
        <!-- pics -->
        <view class="fitem">
          <navigator url>
            <image src="/static/uploads/pic_floor03_1@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor03_2@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor03_3@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor03_4@2x.png" />
          </navigator>
          <navigator url>
            <image src="/static/uploads/pic_floor03_5@2x.png" />
          </navigator>
        </view>
      </view>
    </view>
    <!-- 底部提示 -->
    <view class="end">
      <text>我是有底线的！</text>
    </view>
    <!-- 回到顶部 -->
    <view @click="goTop" class="goTop icon-top" v-if="isShowtop"></view>
  </view>
</template>

<script>
import request from '@/utils/request.js'
export default {
  data() {
    return {
      // 不滚动
      pageHeight: 'auto',
      // 轮播图
      swiperList: [],
      // 导航
      cateList: [],
      // 楼层
      footerList: [],
      // 控制是否回顶按钮
      isShowtop: false
    }
  },
  // 跳转时传参
  onLoad() {
    this.testApi()
    this.getswiperList()
    this.getcateList()
  },
  // 下拉刷新 真机中需要接口调用完,关闭下拉刷新效果
  onPullDownRefresh() {
    Promise.all([this.testApi(), this.getswiperList(), this.getcateList()]).then(() => {
      // 执行完停止loading
      uni.stopPullDownRefresh()
    })
  },
  // 页面滚动的时候执行
  onPageScroll(e) {
    // console.log('页面滚动高度', e.scrollTop)
    if (e.scrollTop > uni.getSystemInfoSync().windowHeight / 2) {
      this.isShowtop = true
    } else {
      this.isShowtop = false
    }
  },
  methods: {
    disScroll(val) {
      // console.log(val)
      this.pageHeight = val
    },
    async testApi() {
      const res = await request({
        url: '/api/public/v1/home/swiperdata'
      })
      // console.log('后台数据', res)
    },
    // 获取轮播图
    async getswiperList() {
      const { data: res } = await request({
        url: '/api/public/v1/home/swiperdata'
      })
      // console.log('轮播图', res)
      this.swiperList = res
    },
    //获取导航
    async getcateList() {
      const { data: res } = await request({
        url: '/api/public/v1/home/catitems'
      })
      // console.log('功能导航', res)
      this.cateList = res
    },
    // 回到顶部
    goTop() {
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 300
      })
    }
    //控制回到顶部按钮显示
  }
}
</script>

<style lang="scss">
// 首页
.index {
  // 轮播
  .slider {
    swiper,
    image {
      width: 750rpx;
      height: 340rpx;
    }
  }
  // 功能导航
  .navs {
    display: flex;
    padding: 30rpx;
    background: #fff;
    justify-content: space-between;
    navigator {
      width: 128rpx;
      height: 140rpx;
    }
  }

  // 栏目楼层
  .floor {
    .ftitle {
      padding-top: 30rpx;
      background: #f4f4f4;
      image {
        width: 750rpx;
        height: 60rpx;
      }
    }
    // pics
    .fitem {
      padding: 20rpx 16rpx 10rpx;
      overflow: hidden;
      navigator {
        float: left;
        width: 193rpx;
        height: 188rpx;
        margin-left: 10rpx;
        margin-bottom: 10rpx;
      }
      navigator:nth-child(1) {
        width: 232rpx;
        height: 386rpx;
        margin-left: 0;
      }
      navigator:nth-child(2),
      navigator:nth-child(5) {
        width: 273rpx;
        height: 188rpx;
      }
    }
    &:first-child {
      .fitem {
        navigator {
          width: 233rpx;
        }
      }
    }
  }
  .end {
    text-align: center;
    font-size: 24rpx;
    color: #999;
  }
  .goTop {
    position: fixed;
    bottom: 30rpx;
    /* #ifdef H5 */
    bottom: 65px;
    /* #endif */
    right: 30rpx;

    display: flex;
    justify-content: center;
    align-items: center;
    width: 100rpx;
    height: 100rpx;
    font-size: 48rpx;
    color: #666;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.8);
  }
}
</style>
